import os, django, sys, random, time
sys.path.insert(0, "../")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ayc_bbs.settings')
django.setup()
from django.conf import settings
from user_app.models import AccountInfo
from forum.models import Topic, Comment, ActivityTopic, BoardTopic
from utils import time_util
from back.models import AutomaticPostComment, AutomaticCommentList, AutomaticAccountList
from django.db.models import F


def bat_post_comment(phone_list):
    today = time_util.now_str()[:10]
    automatic_topic = AutomaticPostComment.objects.all()
    comment_list = AutomaticCommentList.objects.all().values_list('content', flat=True)
    account_list = AutomaticAccountList.objects.all().values_list('account_id', flat=True)
    for tc in automatic_topic:
        # 随机的TopicID可能存在帖子已经被删的情况
        topic_id = tc.topic_id
        if not Topic.objects.filter(id=topic_id).exists():
            # tc.delete()
            continue
        content = comment_list[random.randint(0, len(comment_list) - 1)]
        random_hours = random.randint(0, 23)
        random_second = random.randint(0, 59)
        random_minus = random.randint(0, 59)
        full_time_str = "%s %.2d:%.2d:%.2d" % (today, random_hours, random_second, random_minus)
        account_id = account_list[random.randint(0, len(account_list) - 1)]
        # print("date:%s---account_id:%s---topic:%s---content:%s" % (full_time_str, account_id, topic_id, content))
        cm = Comment()
        cm.topic_id = topic_id
        cm.content = content
        cm.category = tc.category
        cm.is_manual = 1
        cm.created_by_id = account_id
        cm.created_on = full_time_str
        cm.save()
        # # 修改Topic评论数
        if tc.category == 0:
            tpc = Topic.objects.get(id=topic_id)
            tpc.num_replies = F('num_replies') + 1
            tpc.save(update_fields=['num_replies'])
        elif tc.category == 1:
            tpc = ActivityTopic.objects.get(id=topic_id)
            tpc.num_replies = F('num_replies') + 1
            tpc.save(update_fields=['num_replies'])
        elif tc.category ==2:
            tpc = BoardTopic.objects.get(id=topic_id)
            tpc.num_replies = F('num_replies') + 1
            tpc.save(update_fields=['num_replies'])

        time.sleep(0.2)


def init_account_info(phone_list):
    ac = AccountInfo.objects.filter(mobile_phone__in=phone_list)
    for item in ac:
        aa = AutomaticAccountList()
        aa.account_id = item.id
        aa.save()


if __name__ == '__main__':
    if not settings.DEBUG:
        # 线上用户
        phone_list = [
            "17912341111",
            "17912341112",
            "17912341113",
            "17912341114",
            "17912341115",
            "17912341116",
            "17912341117",
            "17912341118",
            "17912341119",
            "17912341121",
            "17912341122",
            "17912341123",
            "17912341124",
            "17912341125",
            "17912341126",
            "17912341127",
            "17912341128",
            "17912341129",
            "17912341131",
            "17912341132",
            "17912345678"
        ]
    else:
        # 测试用户
        phone_list = [
            '16823456789',
            '16833456789',
            '16843456789',
            '16853456789',
            '16863456789',
            '16873456789',
            '16883456789',
            '16893456789',
            '17822456789',
            '17832456789',
            '17842456789',
            '17852456789',
            '17862456789',
            '17872456789',
            '17892456789',
            '17833456789',
            '17843456789',
            '17853456789',
            '17863456789'
        ]
    #init_account_info(phone_list)
    with time_util.Timer() as t:
        bat_post_comment(phone_list)
        print(time_util.now_str())
